﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo.Chess.Rule
{
    /// <summary>
    /// 移动是否合法规则
    /// </summary>
    public class CheckMoveChessPieceRule : IRule
    {
        private ChessBoard _chessBoard;
        private Player _player;
        private ChessPiece _chessPiece;
        private Dir _dir;
        
        public CheckMoveChessPieceRule(ChessBoard chessBoard, Player player, ChessPiece chessPiece,Dir dir) {
            this._chessBoard = chessBoard;
            this._player = player;
            this._chessPiece = chessPiece;
            this._dir = dir;
        }

        public bool Execute()
        {
            var pos = _chessPiece.GetPosition();
            int row = pos.row;
            int col = pos.col;

            switch (_dir) {
                case Dir.LEFT:
                    col--;
                    break;
                case Dir.RIGHT:
                    col++;
                    break;
                case Dir.TOP:
                    row--;
                    break;
                case Dir.BOTTOM:
                    row++;
                    break;
            }
            //检查位置是否合法
            if (!_chessBoard.CheckPositionVaild(row, col)) return false;

            //检查此处是否有棋子
            if (_chessBoard.IsEmpty(row, col)) return true;

            //检查是否是自己的棋子
            if (_player.GetChessPiece(row, col) != null) return false;

            //是对方的棋子，检查是否能吃掉
            //检查跳过去的位置
            switch (_dir)
            {
                case Dir.LEFT:
                    col--;
                    break;
                case Dir.RIGHT:
                    col++;
                    break;
                case Dir.TOP:
                    row--;
                    break;
                case Dir.BOTTOM:
                    row++;
                    break;
            }

            //检查位置是否合法
            if (!_chessBoard.CheckPositionVaild(row, col)) return false;

            //检查此处是否有棋子
            if (_chessBoard.IsEmpty(row, col)) return true;

            return false;
        }
    }
}
